<!--
 * new page
 * @author: liuxin
 * @since: 2024-08-16
 * create_admin.vue
-->
<template>
	<el-dialog v-model="dialogFormVisible" title="申请出库" width="600px" align-center draggable>
		<div class="product-name">您申请出库的产品是: &nbsp;&nbsp;{{ formData.product_name }}</div>
		<div class="product-name">该产品的库存还有: &nbsp;&nbsp;{{ formData.product_inwarehouse_number }}
		</div>
		<div class="dialog-content">
			<el-form ref="ruleFormRef" :model="formData" :label-position="labelPosition" :rules="rules"
				label-width="120px">
				<el-form-item label="申请出库编号" prop="product_out_id">
					<el-input v-model="formData.product_out_id" />
				</el-form-item>
				<el-form-item label="出库数量" prop="product_out_number">
					<el-input v-model="formData.product_out_number" />
				</el-form-item>
				<el-form-item label="出库申请人" prop="product_out_apply_person">
					<el-input v-model="formData.product_out_apply_person" disabled />
				</el-form-item>
				<el-form-item label="产品单价" prop="product_single_price">
					<el-input v-model="formData.product_single_price" disabled />
				</el-form-item>

				<el-form-item label="申请出库备注" prop="apply_memo">
					<el-input v-model="formData.apply_memo" :row="2" type="textarea" />
				</el-form-item>
			</el-form>
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="addProduct"
					:disabled="formData.product_inwarehouse_number < formData.product_out_number">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onBeforeUnmount } from "vue"
import { bus } from "@/utils/mitt.js"
import { applyOutProduct } from '@/api/product.js'
import { ElMessage } from 'element-plus'

bus.on('applyProductId', (row: any) => {
	formData.id = row.id
	formData.product_inwarehouse_number = row.product_inwarehouse_number
	formData.product_name = row.product_name
	formData.product_single_price = row.product_single_price
})
const labelPosition = ref('left')
const title = ref()

/* 	// 获取部门数据
	const departmentData = ref()
	const getDepartmentFn = async () => {
		departmentData.value = await getDepartment()
	}
	getDepartmentFn() */
interface FormData {
	id: number | null,
	product_name: string,
	product_out_id: number | null,
	product_inwarehouse_number: number | null,
	product_single_price: number | null,
	product_out_number: number | null,
	product_out_apply_person: string | null,
	apply_memo: string
}

const formData: FormData = reactive({
	id: null,
	product_name: '',
	product_out_id: null,
	product_inwarehouse_number: null,
	product_single_price: null,
	product_out_number: null,
	product_out_apply_person: localStorage.getItem('name'),
	apply_memo: ''
})

// 表单规则
const rules = reactive({
	product_out_id: [
		{
			required: true,
			message: "请输入申请出库编号",
			trigger: "blur",
		},
	],
	product_out_number: [
		{
			required: true,
			message: "请输入申请出库数量",
			trigger: "blur",
		},
	],
	product_out_apply_person: [
		{
			required: true,
			message: "请输入申请人",
			trigger: "blur",
		},
	],


});
const emit = defineEmits(['success'])
// 产品入库
const addProduct = async () => {
	const res = await applyOutProduct(formData)
	if (res.status == 0) {
		ElMessage({
			message: '产品申请出库成功',
			type: 'success'
		})
		emit('success')
		// bus.emit('adminDialogOff', 1)
		dialogFormVisible.value = false
	} else {
		ElMessage.error('产品申请出库失败')
		dialogFormVisible.value = false
	}
}
// 弹窗开关
const dialogFormVisible = ref(false)
// 打开创建管理员弹窗
const open = () => {
	dialogFormVisible.value = true
}
defineExpose({
	open
})

// 取消订阅/监听
onBeforeUnmount(() => {
	bus.all.clear()
})
</script>

<style scoped lang="scss">
.dialog-content {
	display: flex;
	padding: 0px 30px 20px 30px;
}

.product-name {
	display: flex;
	justify-content: center;
	font-size: 16px;
	margin: 10px;
	color: #333;
}

:deep(.el-form-item) {
	margin: 30px;
}
</style>